<?php
/**
 * This class is the model for the comment table in the database.
 * It is used to access the database table directly.  The comment 
 * table should not be accessed anywhere else in the project.
 * 
 * @author Matt Makins
 * 
 * @param $id
 * @param $author
 * @param $comment
 * @param $post_id
 * @param $submit_date
 * 
 */
class Comment extends BaseModel{
	
	private $id;
	private $author;
	private $comment;
	private $post_id;
	private $submit_date;
	
	public function Comment($id, $author, $comment, $post_id, $submit_date){
		
		$this->id = $id;
		$this->author = $author;
		$this->comment = $comment;
		$this->post_id = $post_id;
		$this->submit_date = $submit_date;
	}

	/**
	 * add a comment to the comment database.  This comment will
	 * be associated with a specific post
	 * 
	 * @param $author
	 * @param $comment
	 * @param $post_id
	 * 
	 * @return boolean
	 */	
	public static function add($author, $comment, $post_id){
		
		$a = new FormItem($author, "Name");
		$c = new FormItem($comment, "Comment");
		
		if(self::validate_presence_of($a,$c)){

			$date = date("Y-m-d H:m:s");

			self::connect();
			mysql_query("INSERT INTO comment (author, text, date, post_id) VALUES" .
					" ('$author', '$comment', '$date', '$post_id')");
			mysql_close();
			return true;
		}
		return false;
	}
	
	public static function findArticleComments($post_id){
		
		self::connect();
		$comments = array();
		$find = mysql_query("SELECT * FROM comment WHERE post_id = '$post_id'");
		while ($row = mysql_fetch_array($find)){
 			$i = $row['id'];
 			$a = $row['author'];
 			$text = $row['text'];
 			$post_id = $row['post_id'];
 			$d = $row['date'];
 			array_push($comments, new Comment($i,$a, $text, $post_id, $d));
 		}
		return $comments;
	}
		
	public function getId(){
		return $this->id;
	}

	public function getAuthor(){
		return $this->author;
	}	
	
	public function getComment(){
		return $this->comment;
	}
	
	public function getPostId(){
		return $this->post_id;
	}
	
	public function getSubmitDate(){
		return $this->submit_date;
	}
}
?>
